package com.mike.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.mike.dao.BaseDao;
import com.mike.dao.t_empDao;
import com.mike.pojo.Page;
import com.mike.pojo.T_emp;




@Repository("empDao")
public class t_empDaoImpl  extends BaseDao implements t_empDao{
	
	
	@Override
	public void insert(T_emp emp){
		
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		
		 session.save(emp);
		//Serializable a =!null回头测试
		 
		trans.commit();
		//session.close();


	}
	
	@Override
	public void delete(T_emp emp){
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		
		session.delete(emp);
		
		trans.commit();
		session.close();
	}
	
	@Override
	public void update(T_emp emp){
		Session session = getSession();
				Transaction trans = session.beginTransaction();
				session.saveOrUpdate(emp);
				
		trans.commit();
		session.close();
	}
	
	//分页查
	@Override
	public List<T_emp> queryByList(int page, int rows){
		
		Session session = getSession();
		
		List<T_emp> emps = session.createQuery("from T_emp")
				.setFirstResult((page-1)*rows)
				.setMaxResults(rows)
				.list();

		session.close();
		return emps;
	}
	@Override
	public int getTotalNum(){//总条数
		
		Session session = getSession();
		
		 String hql="select count(*) from T_emp";
		 Query query = session.createQuery(hql);
		int count=((Long) query.setCacheable(true).uniqueResult()).intValue();//此处用Long类型进行转换
		
		 session.close();
		return count;
	}
	
	@Override
	public Page<T_emp> findByPage(int pageNo,int rows){//分页
		Page<T_emp> page = new Page<>();
		page.setPageSize(rows);
		page.setPageNo(pageNo);
		page.setTotalNum(getTotalNum());
		page.setList(queryByList(page.getPageNo(),rows));		
		return page;
		
	}
	
	
	
	
	
	
	
	
	@Override
	public T_emp findById(int empno){
		System.out.println("进入findByID");
		System.out.println("empno等于"+empno);
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String hql = "from T_emp where empno=?";
		T_emp emp = (T_emp) session.createQuery(hql).setParameter(0, empno).uniqueResult();
		trans.commit();
		session.close();
		return emp;
		
	}

}

